/* global window */
import modelExtend from 'dva-model-extend'
import pathToRegexp from 'path-to-regexp'
import queryString from 'query-string'
import * as basicData from 'services/basicData.js'
import { list, create, update, remove, query } from 'services/adminCompanyAccount'
import { message } from 'antd'
import { pageModel } from './common'


export default modelExtend(pageModel, {
  namespace: 'adminCompanyAccountData',

  state: {
    currenciesData: [],

  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((pathname) => {
        const match = pathToRegexp('/admincompanyaccount').exec(pathname.pathname)
        const payload = queryString.parse(location.search) || { page: 1 }// 查询
        if (match) {
          dispatch({ type: 'list', payload })
          dispatch({ type: 'iniBasicData' })
        }
      })
    },
  },

  effects: {

    * list ({ payload }, { call, put }) {
      const data = yield call(list, payload)
      const listData = data.data
      if (data.success) {
        yield put({
          type: 'updateState',
          payload: {
            list: listData.data,
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: listData.total,
            },
          },
        })
      }
    },

    * delete ({ payload }, { call, put, select }) {
      const data = yield call(remove, { ...payload })
      const { selectedRowKeys } = yield select(_ => _.adminCompanyAccountData)
      if (data.success) {
        message.success('delete success')
        yield put({ type: 'updateState', payload: { selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload) } })
        yield put({ type: 'list', payload: { page: 1 } })
      } else {
        message.error('delete failed')
        throw data
      }
    },

    * create ({ payload }, { select, call, put }) {
      const data = yield call(create, payload)
      if (data.success) {
        message.success('create success')
        yield put({ type: 'hideModal' })
        yield put({ type: 'list', payload: { page: 1 } })
      } else {
        message.error('create failed')
        throw data
      }
    },

    * update ({ payload }, { call, put }) {
      const data = yield call(update, payload)
      if (data.success) {
        message.success('update success')
        yield put({ type: 'hideModal' })
        yield put({ type: 'list', payload: { page: 1 } })
      } else {
        message.error('update failed')
        throw data
      }
    },

    * showModal ({ payload }, { call, put }) {
      yield put({
        type: 'updateModalState',
        payload: { ...payload },
      })
    },

    * iniBasicData ({ payload = {} }, { call, put }) {
      const currenciesRes = yield call(basicData.listall, { type: 'Currencies' })
      if (currenciesRes.success) {
        yield put({
          type: 'updateState',
          payload: {
            currenciesData: currenciesRes.data,
          },
        })
      }
    },

  },

  reducers: {
  },
})
